package svgedit.gui.actions;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.util.Locale;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JToggleButton;

import svgedit.gui.Frame;

/**
 * Modifies the frame to display the correct language, based off each languages
 * ResourceBundle.
 * 
 * @author SARJ3
 */
public class LanguageAction
	extends AbstractAction
{
    private static final long serialVersionUID = -3154784843976421744L;
    private String language;
    private Frame frame;

    /**
     * Constructs a LanguageAction taking the main frame and the language string
     * 
     * @param frame
     * @param language
     */
    public LanguageAction(Frame frame, String language)
    {
	super(language);
	this.frame = frame;
	this.language = language;
    }

    @Override
    public void actionPerformed(ActionEvent arg0)
    {
	if (language == "English")
	    frame.setLocale(new Locale("en", "AU"));
	else
	    if (language == "German")
		frame.setLocale(new Locale("de", "DE"));
	    else
		if (language == "Japanese")
		    frame.setLocale(new Locale("ja", "JP"));
	setFrameText();
    }

    /**
     * Updates the frames text elements to match the specified language.
     */
    public void setFrameText()
    {
	// Frame title
	frame.setTitle(frame.getLanguageValue("s0"));

	// File Menu
	frame.getJMenuBar().getMenu(0).setText(frame.getLanguageValue("s1"));
	frame.getJMenuBar().getMenu(0).getItem(0)
		.setText(frame.getLanguageValue("s2"));
	frame.getJMenuBar().getMenu(0).getItem(1)
		.setText(frame.getLanguageValue("s3"));
	frame.getJMenuBar().getMenu(0).getItem(3)
		.setText(frame.getLanguageValue("s4"));
	frame.getJMenuBar().getMenu(0).getItem(4)
		.setText(frame.getLanguageValue("s5"));
	frame.getJMenuBar().getMenu(0).getItem(5)
		.setText(frame.getLanguageValue("s30"));
	frame.getJMenuBar().getMenu(0).getItem(7)
		.setText(frame.getLanguageValue("s6"));
	frame.getJMenuBar().getMenu(0).getItem(8)
		.setText(frame.getLanguageValue("s7"));
	frame.getJMenuBar().getMenu(0).getItem(10)
		.setText(frame.getLanguageValue("s11"));

	// set submenu language
	frame.getLangMenu().getItem(0).setText(frame.getLanguageValue("s8"));
	frame.getLangMenu().getItem(1).setText(frame.getLanguageValue("s9"));
	frame.getLangMenu().getItem(2).setText(frame.getLanguageValue("s10"));

	// Edit Menu
	frame.getJMenuBar().getMenu(1).setText(frame.getLanguageValue("s12"));
	frame.getJMenuBar().getMenu(1).getItem(0)
		.setText(frame.getLanguageValue("s13"));
	frame.getJMenuBar().getMenu(1).getItem(1)
		.setText(frame.getLanguageValue("s14"));
	frame.getJMenuBar().getMenu(1).getItem(3)
		.setText(frame.getLanguageValue("s15"));
	frame.getJMenuBar().getMenu(1).getItem(5)
		.setText(frame.getLanguageValue("s16"));
	frame.getJMenuBar().getMenu(1).getItem(6)
		.setText(frame.getLanguageValue("s17"));
	frame.getJMenuBar().getMenu(1).getItem(8)
		.setText(frame.getLanguageValue("s18"));

	// Insert Menu
	frame.getJMenuBar().getMenu(2).setText(frame.getLanguageValue("s19"));
	frame.getJMenuBar().getMenu(2).getItem(0)
		.setText(frame.getLanguageValue("s20"));
	frame.getJMenuBar().getMenu(2).getItem(1)
		.setText(frame.getLanguageValue("s21"));
	frame.getJMenuBar().getMenu(2).getItem(2)
		.setText(frame.getLanguageValue("s22"));

	// Toolbar
	Component[] toolbarComponents;
	toolbarComponents = frame.getJToolBar().getComponents();

	// Toolbar (File Buttons)
	((JButton) toolbarComponents[0]).setText(frame.getLanguageValue("s2"));
	((JButton) toolbarComponents[1]).setText(frame.getLanguageValue("s3"));
	((JButton) toolbarComponents[2]).setText(frame.getLanguageValue("s4"));

	// Toolbar (Insert Buttons)
	JToggleButton[] toolbarInsertButtons = frame.getToolbarButtons();
	toolbarInsertButtons[0].setText(frame.getLanguageValue("s20"));
	toolbarInsertButtons[1].setText(frame.getLanguageValue("s21"));
	toolbarInsertButtons[2].setText(frame.getLanguageValue("s22"));

	// Toolbar (Labels)
	((JLabel) toolbarComponents[8]).setText(frame.getLanguageValue("s23")
		+ ":");
	((JLabel) toolbarComponents[10]).setText(frame.getLanguageValue("s24")
		+ ":");
	((JLabel) toolbarComponents[12]).setText(frame.getLanguageValue("s25")
		+ ":");

	frame.repaint();
    }
}
